package com.lisx.module_matters.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.lib_data.entity.db.MattersDbEntity;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.lisx.module_matters.R;
import com.lisx.module_matters.databinding.ItemMattersBinding;
import com.lisx.module_matters.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class MattersItemAdapter extends RecyclerView.Adapter<MattersItemAdapter.MyViewHolder> {

    List<MattersDbEntity> mattersDbEntities = new ArrayList<>();
    String tabTitle;

    public MattersItemAdapter(String tabTitle) {
        this.tabTitle = tabTitle;
    }

    public void setData(List<MattersDbEntity> mattersDbEntities) {
        List<MattersDbEntity> zdList = new ArrayList<>();
        //置顶
        for (int i = 0; i < mattersDbEntities.size(); i++) {
            MattersDbEntity mattersDbEntity = mattersDbEntities.get(i);
            if (mattersDbEntity.getIs_top()) {
                mattersDbEntities.remove(i);
                i--;
                zdList.add(mattersDbEntity);
            }
        }
        this.mattersDbEntities.clear();
        if (zdList.size() > 0) {
            this.mattersDbEntities.addAll(zdList);
        }
        this.mattersDbEntities.addAll(mattersDbEntities);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMattersBinding binding = ItemMattersBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MattersDbEntity mattersDbEntity = mattersDbEntities.get(position);
        holder.binding.tvTitle.setText(mattersDbEntity.getMatters_name());
//        holder.binding.tvTime.setText(TimeUtils.getMattersTime(mattersDbEntity.getStart_time(), mattersDbEntity.getEnd_time()));
        if (mattersDbEntity.getIs_top()) {
            holder.binding.tvTime.setText("置顶");
        } else {
            holder.binding.tvTime.setText(tabTitle);
        }
        if (mattersDbEntity.getIs_complete()) {
            holder.binding.ivCheck.setImageResource(R.drawable.ic_shixiang_done2);
            holder.binding.vLine.setVisibility(View.VISIBLE);
            holder.binding.tvTitle.setTextColor(Color.parseColor("#FF81818E"));
        } else {
            holder.binding.tvTitle.setTextColor(Color.parseColor("#FF030319"));
            holder.binding.vLine.setVisibility(View.GONE);
            holder.binding.ivCheck.setImageResource(R.drawable.ic_shixiang_nor);
        }

        holder.binding.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onUpdate(mattersDbEntity, 1);
                }
            }
        });
        holder.binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDelete(mattersDbEntity);
                }
            }
        });
        holder.binding.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onEdit(mattersDbEntity);
                }
            }
        });
        holder.binding.ivZhiding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onUpdate(mattersDbEntity, 2);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mattersDbEntities.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemMattersBinding binding;

        public MyViewHolder(@NonNull ItemMattersBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onUpdate(MattersDbEntity mattersDbEntity, int update);

        void onDelete(MattersDbEntity mattersDbEntity);

        void onEdit(MattersDbEntity mattersDbEntity);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
